/*
 * Tomer Nuni 300585312
 * Idan Meshita 300043726
 */
import java.awt.Point;


public class TransformationManager {

	
	private double [][] transformMatrix=Matrix.IDENTITY_MATRIX;
	
	private double [][] tempTransformMatrix=Matrix.IDENTITY_MATRIX;

	public double[][] getTransformMatrix() {
		return transformMatrix;
	}
	

	public double[][] getSelectedTranformationMatix() {
		return tempTransformMatrix;
	}
	
	public void resetTranformationMatix() {
		transformMatrix=Matrix.IDENTITY_MATRIX;
	}
	
	public void resetTempTranformationMatix() {
		tempTransformMatrix=Matrix.IDENTITY_MATRIX;
	}
	public void addTempTranformationMatixToOverallTransformation() {
		transformMatrix=Matrix.multiplyMatrix(tempTransformMatrix, transformMatrix);
		tempTransformMatrix=Matrix.IDENTITY_MATRIX;
	}
	
	public void createScaleMatrix(Point start,Point end,Point center){
		//calculate distatnce
		double r1 = Math.sqrt((start.x-center.x)*(start.x-center.x)+(start.y-center.y)*(start.y-center.y));
		double r2 = Math.sqrt((end.x-center.x)*(end.x-center.x)+(end.y-center.y)*(end.y-center.y));
		
		double m1 [][]= {{1,0,center.x},
						{0,1,center.y},
						{0,0,1}};
		double m2 [][]= {{r2/r1,0,0},
						{0,r2/r1,0},
						{0,0,1}};
		double m3 [][]= {{1,0,-center.x},
						{0,1,-center.y},
						{0,0,1}};

		
		tempTransformMatrix= Matrix.multiplyMatrix(m1, Matrix.multiplyMatrix(m2, m3));
	}

	public void createRotationMatrix(Point start, Point end,Point center) {
		//calculate distance
		Point v1= new Point(center.x-start.x,center.y-start.y);
		Point v2= new Point(center.x-end.x,center.y-end.y);
		

		double a1 = calculateAngle(v1, new Point(1,0));
		double a2 = calculateAngle(v2, new Point(1,0));
		
		if (v1.y<=0)
			a1=-a1;
		if (v2.y<=0)
			a2=-a2;
			
		double angle = a1-a2;
		
		double cosa = Math.cos(angle);
		double sina = Math.sin(angle);
		double m1 [][]= {{1,0,center.x},
						{0,1,center.y},
						{0,0,1}};
		double m2 [][]= {{cosa,sina,0},
						{-sina,cosa,0},
						{0,0,1}};
		double m3 [][]= {{1,0,-center.x},
						{0,1,-center.y},
						{0,0,1}};

		
		tempTransformMatrix= Matrix.multiplyMatrix(m1, Matrix.multiplyMatrix(m2, m3));
		//tempTransformMatrix=m2;
		
	}

	public void createTranslateMatrix(Point start, Point end) {
		double dx = end.x-start.x;
		double dy = end.y-start.y;



		double m3 [][]= {{1,0,dx},
						{0,1,dy},
						{0,0,1}};
		tempTransformMatrix=m3;
	}
	
	private double calculateAngle(Point v1,Point v2){
		double sizeV1=Math.sqrt((v1.x*v1.x)+(v1.y*v1.y));
		double sizeV2=Math.sqrt((v2.x*v2.x)+(v2.y*v2.y));
		double v1v2=(v1.x*v2.x)+(v1.y*v2.y);
		
		return Math.acos( v1v2/(sizeV1*sizeV2));
	}


	public void reflectOverALine(Point p1, Point p2) {
		//define the two vectors of the new system
		double ux= p2.x-p1.x;
		double uy= p2.y-p1.y;
		
		double size = Math.sqrt(ux*ux+uy*uy);
		ux= ux/size;
		uy= uy/size;
		
		double vx= -uy;
		double vy= ux;
		
		
		
		//Point v = new Point(p2.x-p1.x,p2.y-p1.y );
		//v.x =
		double [][] m = Matrix.IDENTITY_MATRIX;
		//make move to the point matrix
		double m1 [][]= {{1,0,-p1.x},
						{0,1,-p1.y},
						{0,0,1}};
		m= Matrix.multiplyMatrix(m1,m);
		//make axis chagne matrix
		
		double m2[][] = {{ux,uy,0},
						{vx,vy,0},	
						{0,0,1}};
		m= Matrix.multiplyMatrix(m2,m);
		//make reflection matrix
		double m3 [][]= {{1,0,0},
						{0,-1,0},
						{0,0,1}};
		m= Matrix.multiplyMatrix(m3,m);
		//make axis chagne matrix
		
		double m4[][] = {{ux,vx,0},
						{uy,vy,0},	
						{0,0,1}};
		m= Matrix.multiplyMatrix(m4,m);
		//make move to the point matrix
		double m5 [][]= {{1,0,p1.x},
						{0,1,p1.y},
						{0,0,1}};
		m= Matrix.multiplyMatrix(m5,m);
		//move the screen down after -y
		tempTransformMatrix= m;
		addTempTranformationMatixToOverallTransformation();
		resetTempTranformationMatix();
		
	}


}

